package com.example.demo.demo.jdbc;

import java.sql.*;

/**
 * jdbc工具类(statement)
 *
 * @author: htc
 * @date: Created in 16:19 2018/11/2.
 */
public class JdbcUtil {
    final static String url = "jdbc:postgresql://10.110.91.114/idata_meta?&tcpKeepAlive=true&autoReconnect=true";
    final static String user = "postgres";
    final static String pwd = "mmp&H!^&zHCO8pR";

    static {
        //注册驱动程序
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动程序注册出错");
        }
    }

    /**
     * 抽取获取连接对象的方法
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, pwd);
    }

    /***
     * 释放资源的方法
     */
    public static void closeResource(Connection connection, Statement statement) throws SQLException {
        if (null != statement) {
            statement.close();
        }
        if (null != connection) {
            connection.close();
        }
    }

    /***
     * 释放资源的方法
     */
    public static void closeResource(Connection connection, Statement statement, ResultSet rs) throws SQLException {
        if (null != rs) {
            rs.close();
        }
        closeResource(connection, statement);
    }
}
